<?php
/**
 * User: onwer
 * Date: 11-11-28
 * Time: 下午11:41
 */
include_once 'Table.php';
include_once 'Manage.php';
include_once 'File.php';

class Publish extends Manage
{
    public  $pagesize=-1;
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 添加数据
     * @param  $mid int 目录ID
     * @param  $param array 对应的键值
     * @return bool
     */
    public function add($mid, $param)
    {
        $param = $this->handleparam( $mid, $param);
        if(!$param){
            return false;
        }

        $curtablearr = $this->querytable($mid);

        if($this->insert($param, $curtablearr[0]['table_name'])){
            $generation=new Generation($this);
            $generation->makefile($mid);
            return true;
        }else{
            return false;
        }

    }

    public function alt($mid, $param, $id)
    {

        $param = $this->handleparam( $mid, $param);
        $curtablearr = $this->querytable($mid);

        if($this->update($param, sprintf('id=%d',$id), $curtablearr[0]['table_name'])){
            $generation=new Generation($this);
            $generation->makefile($mid);
            return true;
        }else{
            return false;
        }


    }

    public function del($mid, $id)
    {
        $curtablearr = $this->querytable($mid);
        return $this->delete(sprintf('id=%d',$id),$curtablearr[0]['table_name']);
    }


    protected  function handleparam( $mid, $param)
    {
        $param['cat_id'] = $mid;
        $curdirarr = $this->querymid($mid);
        if (!empty($_FILES)) {
            foreach ($_FILES as $key => $val) {
                //$matchs[1] 文件类型 、 $matchs[2] 字段key
                preg_match('/^(.*?)_(.*?)$/', $key, $matchs);

                if ($val['name']) {
                    $newfile = new File($val);
                    if ($newfile->checkfile($matchs[1])) {
                        if ($path = $newfile->save(SITEABSDIR . $curdirarr[0]['file_path'])) {
                            $param[$matchs[2]] = $path;
                        }else{
                            $this->errorinfo[2]=$newfile->errorinfo;
                            return false;
                        }
                    }else{
                        $this->errorinfo[2]=$newfile->errorinfo;
                        return false;
                    }
                }

            }

        }
        return $param;
    }


    /**
     * @param $mid
     * @param $curpage
     * @param $filedsrc
     * @param string $isdesc 是否as 字段描述 为 '' 时不as
     * @return array
     */
    public function show($mid,$curpage,$filedsrc='limit',$isdesc='as %s'){


        $page=$this->getpage($mid,$curpage);
        $pagesize=$page->pagesize;
        $curpage=$page->getcurpage();
        $limit=sprintf('order by id desc limit %d,%d',($curpage-1)*$pagesize,$pagesize);
        return $this->queryspec( $mid ,-1,$limit,$filedsrc,$isdesc);
    }
    protected function getpagesize($mid){
        if($this->pagesize>-1){
            return $this->pagesize;
        }
        $managearr=$this->querymid($mid);
        if(empty($managearr)){
            return 0;
        }
        return $managearr[0]['page_num'];
    }
    public function getpage($mid,$curpage){
        $publistharr = $this->queryspec( $mid,-1,'','count(id) as totalnum');
        if(empty($publistharr)){
            return new Page(0 , $this->getpagesize($mid), $curpage);
        }
        return new Page($publistharr[0]['totalnum'] , $this->getpagesize($mid), $curpage);
    }
    public function getprev($mid,$id,$linktpl){
        $curtablearr = $this->queryspec($mid, -1, '', 'id');
        $curtablelen = count($curtablearr);
        for ($i = 0; $i < $curtablelen; $i++) {
            if ($curtablearr[$i]['id'] == $id && $id-1>0) {
                return sprintf($linktpl,$curtablearr[$i - 1]['id']);
            }
        }
        return '';
    }
    public function getnext($mid,$id,$linktpl){
        $curtablearr = $this->queryspec($mid, -1, '', 'id');
        $curtablelen = count($curtablearr);
        for ($i = 0; $i < $curtablelen; $i++) {
            if ($curtablearr[$i]['id'] == $id && $i + 1 < $curtablelen) {
                return sprintf($linktpl,$curtablearr[$i + 1]['id']);
            }
        }
        return '';
    }
    public function getrand($mid,$num){
        $curtablearr = $this->queryspec($mid, -1, '','*','');
        $tmparr=array();
        $rand_keys = array_rand($curtablearr, $num);
        for($i=0;$i<$num;$i++){
            array_push($tmparr,$curtablearr[$rand_keys[$i]]);
        }
        return $tmparr;
    }
}
